package com.tongguan.paypos.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.tongguan.paypos.R;

import java.util.ArrayList;

/**
 * Created by Joylive on 2016/12/5 .
 */

public class DropDownEditText extends EditText {


    private Context mContext;
    private SpinerPopWindow mSpinerPopWindow;
    private ArrayList<String> dataList = new ArrayList<String>();
    private OnPopItemClickListener itemClickListener;

    public void setShowData(ArrayList<String> datalist){
        dataList.clear();
        this.dataList.addAll(datalist);
        mSpinerPopWindow = new SpinerPopWindow(mContext, listener);
        mSpinerPopWindow.setData(dataList);
    }
    private AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            setText(dataList.get(arg2));
            setSelection(getText().length());
            if(itemClickListener != null){
                itemClickListener.onItemClick(arg2);
            }
            if(mSpinerPopWindow != null && mSpinerPopWindow.isShowing()){
                mSpinerPopWindow.dismiss();
            }
        }
    };

    private int top = 0, right = 0, bottom = 0, left = 0;
    private int borderColor = android.graphics.Color.parseColor("#808080");

    public void SetReadonly(boolean readonly) {
    }

    public DropDownEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        this.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
    }

    /**
     *  弹出pop
     */
    public void setOnClick() {
        //this.setInputType(InputType.TYPE_NULL); // 防止弹出键盘
        mSpinerPopWindow.setWidth(getWidth());
        mSpinerPopWindow.showAsDropDown(this, 0, -3);
    }
    public DropDownEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownEditText);
        boolean isShowDropButton = a.getBoolean(R.styleable.DropDownEditText_isShowDropButton, false);
        boolean isInputEnable = a.getBoolean(R.styleable.DropDownEditText_isInputEnable, true);
        //Drawable button = a.getDrawable(R.styleable.DropDownEditText_button);
        int button = a.getInt(R.styleable.DropDownEditText_button, 0);
        mContext = context;
        if(isShowDropButton){
            if(button == 0){
                this.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
            }else{
                this.setCompoundDrawablesWithIntrinsicBounds(0, 0, button, 0);
            }
        }
        if(!isInputEnable){
            this.setInputType(InputType.TYPE_NULL); // 防止弹出键盘
        }
    }

    public DropDownEditText(Context context) {
        super(context);
        mContext = context;
//		this.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable.down,
//				0);
    }

    public DropDownEditText(Context context, int top, int right, int bottom,
                            int left, int borderColor) {
        super(context);
        mContext = context;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
        this.left = left;
        this.borderColor = borderColor;
//		this.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawable.down,
//				0);
    }

    public TextWatcher textWatcher;

    /**
     * 添加文本改变事件
     *
     * @param _textWatcher
     */
    public void setOnTextChanged(TextWatcher _textWatcher) {
        this.textWatcher = _textWatcher;
        super.addTextChangedListener(this.textWatcher);
    }

    /**
     * 移除文本改变事件
     */
    public void removeTextChanged() {
        super.removeTextChangedListener(this.textWatcher);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    @Override
    public void setCompoundDrawablesWithIntrinsicBounds(int left, int top,
                                                        int right, int bottom) {
        super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
    }

    @SuppressLint("DrawAllocation")
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        // 将边框设为黑色
        paint.setColor(borderColor);
        // 画TextView的4个边
        if (top > 0)
            canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
        if (left > 0)
            canvas.drawLine(0, 0, 0, this.getHeight(), paint);
        if (right > 0)
            canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
                    this.getHeight(), paint);// 边框
        if (bottom > 0)
            canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
                    this.getHeight() - 1, paint);
    }
    public interface OnPopItemClickListener{
        public abstract void onItemClick(int index);
    }
    public void setOnPopItemClickListener(OnPopItemClickListener listener){
        this.itemClickListener = listener;
    }
}
